import type { ConfigEnv, UserConfig } from 'vite';
import { loadEnv } from 'vite';
import path, { resolve } from 'path';
import createVitePlugins from './vite/plugins';
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const root = process.cwd();
  const env = loadEnv(mode, root);
  const isBuild = command === 'build';
  return {
    base: env.VITE_APP_PUBLIC_PATH,
    plugins: createVitePlugins(env, isBuild),
    esbuild: {
      drop: ['console', 'debugger'], // 移除 console 和 debugger
      pure: ['console.log', 'console.warn'] // 可选：移除特定的 console 方法
    },
    build: {
      reportCompressedSize: false,
      minify: true,
      cssCodeSplit: false,
      chunkSizeWarningLimit: 2048,
      rollupOptions: {
        input: {
          main: resolve(__dirname, 'index.html')
        },
        output: {
          manualChunks: {
            vue: ['vue', 'pinia', 'vue-router'],
            antdv: ['ant-design-vue', '@ant-design/icons-vue']
          }
        }
      }
    },
    resolve: {
      alias: {
        '~@': path.join(__dirname, './src'),
        '@': path.join(__dirname, './src'),
        '~': path.join(__dirname, './src/assets'),
        vue: 'vue/dist/vue.esm-bundler.js',
        dayjs: resolve(__dirname, 'node_modules', 'dayjs'),
        'ant-design-vue': resolve(__dirname, 'node_modules', 'ant-design-vue')
      }
    },
    optimizeDeps: {
      include: [
        'ant-design-vue/es/locale/en_US',
        'ant-design-vue/es/locale/zh_CN',
        'store/plugins/expire',
        'ant-design-vue/es/form',
        'dayjs',
        'dayjs/locale/en',
        'dayjs/locale/zh-cn',
        '@ant-design/icons-vue',
        'lodash-es',
        'pinia',
        'vue-router',
        'vue',
        'vue-i18n',
        '@vueuse/core'
      ],
      exclude: ['@antv/x6', '@antv/x6-plugin-scroller', '@antv/x6-vue-shape']
    },
    css: {
      devSourcemap: true,
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: 'true; @import "~/styles/variables.less";'
          },
          // DO NOT REMOVE THIS LINE
          javascriptEnabled: true
        }
      }
    },
    server: {
      port: 4000,
      host: true,
      open: true,
      headers: {
        'Access-Control-Allow-Origin': '*'
        // 'Access-Control-Allow-Credentials': true
      },
      proxy: {
        '/api/back': {
          target: 'http://127.0.0.1:10008/',
          ws: false,
          changeOrigin: true
        },
        '/api': {
          target: 'http://127.0.0.1:10001/',
          ws: false,
          changeOrigin: true
        }
      }
    }
  };
};
